/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.functions;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.osdtsystem.graphs.GraphAccessor;
import org.osdtsystem.values.Location;

/**
 *
 * @param <V> 
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public final class Constant<V>
        implements SingletonFunction<V> {
    private String name;
    private final V value;
    
    public Constant(V value) {
        this.value = value;
    }

    public static Constant<String> create(String s) {
        return new Constant(s);
    }
    
    @Override
    public V singletonFunctionValue(LocalFunction<?, ?> caller,
            FunctionContext context, 
            GraphAccessor accessor) {
        return value;
    }

    @Override
    public List<V> functionValue(LocalFunction<?, ?> caller,
            FunctionContext context, 
            GraphAccessor accessor) {
       Object[] raw = {value}; 
       return Arrays.asList((V[]) raw);
    }
    

    @Override
    public List<Function<?>> subfunctions() {
        return Collections.emptyList();
    }

    @Override
    public List<String> freeVariables() {
        return Collections.emptyList();
    }

    @Override
    public boolean callsNamedFunction() {
        return false;
    }

    @Override
    public boolean callsNamedFunctionLocally() {
        return false;
    }

    @Override
    public byte height() {
        return 0;
    }

    @Override
    public boolean isUnary() {
        return false;
    }

    @Override
    public boolean isConstant() {
        return true;
    }

    @Override
    public String unaryVariable() {
        return null;
    }

    @Override
    public boolean restoreContextState() {
        return true;
    }

    @Override
    public void addLocalNamedFunction(String name) {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void addLocalBoundVariable(String name) {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void addLocalFreeVariable(String name) {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public boolean isFunction() {
        return true;
    }

    @Override
    public String name() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public List<V> functionValue(Location location, GraphAccessor accessor) {
        return functionValue(null, null, accessor);
    }

    @Override
    public String toString() {
        return toString("", "");
    }

    @Override
    public String toString(String prefix) {
        return toString(prefix, prefix);
    }

    @Override
    public String toString(String prefixFirst, String prefixRest) {
        return prefixFirst + "Constant: "
                + (name != null ? "name=" + name.toString() : "")
                + (value != null ? value.toString() : "null") + "\n";
    }

    @Override
    public String toString(String prefix1, String prefix, LocalFunction<?, ?> caller, FunctionContext context, GraphAccessor accessor) {
        return toString(prefix1, prefix);
    }

    @Override
    public boolean callingToString() {
        return false;
    }

    @Override
    public void setCallingToString(boolean caughtException) {
    }

}
